var mainIndexApp = angular.module("mainIndexApp", [ "publicServiceApp", "clusterServiceApp", "ngSelect2"]);
mainIndexApp.controller("mainIndexAppCtrl",[ 
        "$scope",
		"publicService",
		"clusterService",
		"$timeout",
		"$log",
		function($scope, publicService, clusterService, $timeout, $log) {
			$scope.error = "";
			$scope.showQuery = false;
			$scope.clsuterName = null;
			$scope.clusterSelectConfig = {
					minimumResultsForSearch : 1, // 是否打开搜索
					data : [],			 
					placeholder : '如无选项请新增集群'			 // 占位符
			};
			
			/**
			 * 
			 * 功能说明:查询集群下拉框选项
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.queryClusterSelect = function(){
				clusterService.query({}, function(result){
					if(!result.result){
						$scope.error = result.msgTitle;
						$scope.isSuccess = false;
						return false;
					}
					$scope.clusterSelectConfig.data  = new Array();
					angular.forEach(result.dataList, function(data, index, array){
						var option = {
								id : data.name,
								text : data.name
						}
						$scope.clusterSelectConfig.data.push(option);
					});
					ffc.util.angularApply($scope, $timeout);
					
					
					clusterService.queryCurrent({}, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						$scope.clusterName = result.object;
						ffc.util.angularApply($scope, $timeout);
					});
				},function(error){
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			/**
			 * 
			 * 功能说明:查询所有菜单，父子结构展示
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			publicService.queryMenu({}, function(data) {
				if (data.result) {
					$scope.menus = data.dataList;
				}else{
					$scope.error = "查询菜单失败，联系管理员！！";
					$scope.isSuccess = false;
				}
			}, function(error) {
				$scope.error = error;
				$scope.isSuccess = false;
			});
			
			/**
			 * 
			 * 功能说明:查询所有菜单，一级展示
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			publicService.queryAllMenu({}, function(data) {
				if (data.result) {
					$scope.allMenus = data.dataList;
				}else{
					$scope.error = "查询菜单失败，联系管理员！！";
					$scope.isSuccess = false;
				}
			}, function(error) {
				$scope.error = error;
				$scope.isSuccess = false;
			});
		
			/**
			 * 
			 * 功能说明:菜单查询框变化事件
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			$scope.queryFocus = function() {
				 if($scope.menuName.length > 0){
					 $scope.showQuery = true;
				 }else{
					 $scope.showQuery = false;
				 }
			};
		
			/**
			 * 
			 * 功能说明:监听结果
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			$scope.exit = function() {
				publicService.exit({}, function(data) {
					if (data.result) {
						window.location.href = "../login/login.jsp";
					}else{
						$scope.error = "退出失败，联系管理员！！";
						$scope.isSuccess = false;
					}
				}, function(error) {
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			
			
			/**
			 * 
			 * 
			 */
			$scope.selectCluster = function(){
				clusterService.select({name : $scope.clusterName}, function(result){
					if(!result.result){
						$scope.error = result.msgTitle;
						$scope.isSuccess = false;
						return false;
					}
					new MessageDialog().success("切换成功！！！");
					ffc.util.angularApply($scope, $timeout);
				},function(error){
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			
		
			/**
			 * 
			 * 功能说明:监听结果
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			$scope.$watch('isSuccess', function(nv, ov) {
				if (!nv) {
					$scope.isSuccess = true;
					var messageDialog = new MessageDialog().error($scope.error);
					setTimeout(function() {
						if(messageDialog != undefined){
							messageDialog.close();
						}
					}, 2000);
				}
			});
		
			
			
			/**
			 * 
			 * 功能说明:刷新angular的作用域数据
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月20日
			 * 
			 */
			$scope.angularApply = function() {
				if (!$scope.$$phase) {
					$scope.$apply();
				}
			}
			
			
			$(function() {
				$scope.queryClusterSelect();
			});
}]);

/*
 * mainIndexApp.controller("mainUserEditCtrl", [ "$scope", "commonService",
 * "sysService", "$timeout", "$log", function($scope, commonService, sysService,
 * $timeout, $log) { // 添加,修改用户信息 $scope.saveUser = function() { if
 * ($scope.action == "mod") { sysService.updateUser($scope.submitUser,
 * function(result) { if (!result.result) { $scope.isSuccess = false; return
 * false; } $scope.saveSucess(); }, function() { $scope.isSuccess = false; }); }
 * else { sysService.saveUser($scope.submitUser, function(result) { if
 * (!result.result) { $scope.isSuccess = false; return false; }
 * $scope.saveSucess(); }, function() { $scope.isSuccess = false; }); } }
 * 
 * $scope.saveSucess = function() { MESSAGE_DIALOG.alert("保存成功");
 * $scope.$userTableEdit.modal("hide"); $scope.submitUser.id = null,
 * $scope.submitUser.userName = null, $scope.submitUser.userCode = null,
 * $scope.submitUser.password = null, $scope.submitUser.phone = null,
 * $scope.submitUser.rtx = null, $scope.submitUser.orgName = null;
 * $scope.$table.bootstrapTable("selectPage", 1); }
 *  } ]);
 */